package cn.maihe.elg.operation.service.business;

import cn.maihe.elg.operation.model.enums.CenterNoEnum;
import cn.maihe.elg.operation.model.vo.business.ExchangeUserVO;
import cn.maihe.elg.operation.model.vo.business.OrgRelationVO;
import cn.maihe.elg.operation.repository.entity.ExchangeCenterDO;
import cn.maihe.elg.operation.repository.entity.ExchangeCenterVo;
import cn.maihe.elg.operation.repository.mapper.ExchangeCenterMapper;
import cn.maihe.elg.operation.repository.mapper.GuaranteeInstitutionMapper;
import cn.maihe.elg.operation.service.BaseDaoService;
import cn.maihe.framework.starter.base.sql.PageBo;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @Description 交易中心服务
 * @Author WJH
 * @Date 2021/03/25
 */
@Slf4j
@Service
public class ExchangeCenterService extends BaseDaoService<ExchangeCenterDO> {

    @Resource
    private ExchangeCenterMapper exchangeCenterMapper;
    @Resource
    private GuaranteeInstitutionMapper guaranteeInstitutionMapper;

    public PageBo<ExchangeCenterDO> selectPageBySql(int pageNo, int pageSize, ExchangeCenterVo vo) {
        Map<String, Object> paramsMap = toMapAndAddParams(vo);
        return this.selectPageBySql("selectCenterList", "selectCenterCount", paramsMap, pageNo, pageSize);
    }

    /**
     * 是否存在 用户-交易中心 关联记录
     *
     * @param id
     * @return
     */
    public boolean existsCenterLinkedUser(Long id) {
        Map<String, Object> paramsMap = Maps.newHashMap();
        paramsMap.put("id", id);
        return this.selectCountBySql("centerLinkedUserCount", paramsMap) > 0;
    }

    public List<ExchangeUserVO> getOrgCenter(Long userId) {
        Map<String, Object> paramsMap = Maps.newHashMap();
        paramsMap.put("userId", userId);
        return this.selectListBySql("orgCenterList", paramsMap);
    }

    @Transactional(rollbackFor = Exception.class)
    public void saveOrgRelation(OrgRelationVO orgRelationVO) {
        exchangeCenterMapper.deleteOrgCenter(orgRelationVO.getUserId());
        guaranteeInstitutionMapper.deleteOrgInstittion(orgRelationVO.getUserId());
        if (!CollectionUtils.isEmpty(orgRelationVO.getCenter())) {
            exchangeCenterMapper.insertOrgCenter(orgRelationVO.getUserId(), orgRelationVO.getCenter());
        }
        if (!CollectionUtils.isEmpty(orgRelationVO.getInstitution())) {
            guaranteeInstitutionMapper.insertOrgInstittion(orgRelationVO.getUserId(), orgRelationVO.getInstitution());
        }
    }

    public List<ExchangeUserVO> getAccountCenter(Long userId, Long orgUserId) {
        Map<String, Object> paramsMap = Maps.newHashMap();
        paramsMap.put("userId", userId);
        paramsMap.put("orgUserId", orgUserId);
        return this.selectListBySql("accountCenterList", paramsMap);
    }

    public ExchangeCenterDO getCenterByAcceptOrderNo(String acceptOrderNo) {
        Map<String, Object> params = Maps.newHashMap();
        params.put("acceptOrderNo", acceptOrderNo);
        return this.selectOneBySql("getCenterByAcceptOrderNo", params);
    }

    public CenterNoEnum getCenterNoEnumByAcceptOrderNo(String acceptOrderNo) {
        ExchangeCenterDO centerDO = this.getCenterByAcceptOrderNo(acceptOrderNo);
        return CenterNoEnum.resolve(centerDO.getCenterCode());
    }

    public CenterNoEnum getCenterNoEnumById(Long centerId) {
        return CenterNoEnum.resolve(this.selectById(centerId).getCenterCode());
    }
}
